home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Tcl-Tk 8.0 / Pre-installed version / tcl8.0 / compat / strstr.c < prev    next >
Encoding:
Text File  |  1997-08-15  |  1.6 KB  |  69 lines  |  [TEXT/CWIE]

  1. /* 
  2.  * strstr.c --
  3.  *
  4.  *    Source code for the "strstr" library routine.
  5.  *
  6.  * Copyright (c) 1988-1993 The Regents of the University of California.
  7.  * Copyright (c) 1994 Sun Microsystems, Inc.
  8.  *
  9.  * See the file "license.terms" for information on usage and redistribution
  10.  * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
  11.  *
  12.  * SCCS: @(#) strstr.c 1.4 96/02/15 12:08:22
  13.  */
  14.  
  15. /*
  16.  *----------------------------------------------------------------------
  17.  *
  18.  * strstr --
  19.  *
  20.  *    Locate the first instance of a substring in a string.
  21.  *
  22.  * Results:
  23.  *    If string contains substring, the return value is the
  24.  *    location of the first matching instance of substring
  25.  *    in string.  If string doesn't contain substring, the
  26.  *    return value is 0.  Matching is done on an exact
  27.  *    character-for-character basis with no wildcards or special
  28.  *    characters.
  29.  *
  30.  * Side effects:
  31.  *    None.
  32.  *
  33.  *----------------------------------------------------------------------
  34.  */
  35.  
  36. char *
  37. strstr(string, substring)
  38.     register char *string;    /* String to search. */
  39.     char *substring;        /* Substring to try to find in string. */
  40. {
  41.     register char *a, *b;
  42.  
  43.     /* First scan quickly through the two strings looking for a
  44.      * single-character match.  When it's found, then compare the
  45.      * rest of the substring.
  46.      */
  47.  
  48.     b = substring;
  49.     if (*b == 0) {
  50.     return string;
  51.     }
  52.     for ( ; *string != 0; string += 1) {
  53.     if (*string != *b) {
  54.         continue;
  55.     }
  56.     a = string;
  57.     while (1) {
  58.         if (*b == 0) {
  59.         return string;
  60.         }
  61.         if (*a++ != *b++) {
  62.         break;
  63.         }
  64.     }
  65.     b = substring;
  66.     }
  67.     return (char *) 0;
  68. }
  69.